
<!DOCTYPE HTML>
<html lang="" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>java输入输出流 · JAVA学习积累</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        <meta name="author" content="tianwyam">
        
        
    
    <link rel="stylesheet" href="../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-tbfed-pagefooter/footer.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-splitter/splitter.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-pageview-count/plugin.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-anchor-navigation-ex/style/plugin.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-chapter-fold/chapter-fold.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-expandable-chapters-small/expandable-chapters-small.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="java_exception.html" />
    
    
    <link rel="prev" href="java_tree.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
            
                <nav role="navigation">
                


<ul class="summary">
    
    
    
        
        <li>
            <a href="https://blog.csdn.net/mybook201314" target="_blank" class="custom-link">博客Blog</a>
        </li>
    
    

    
    <li class="divider"></li>
    

    
        
        <li class="header">【前言】</li>
        
        
    
        <li class="chapter " data-level="1.1" data-path="../">
            
                <a href="../">
            
                    
                    介绍ABOUT
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">【第一章】Java基础</li>
        
        
    
        <li class="chapter " data-level="2.1" data-path="java.html">
            
                <a href="java.html">
            
                    
                    计算机基础-操作系统-进程-计算机网络
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.2" data-path="java_thread_pool.html">
            
                <a href="java_thread_pool.html">
            
                    
                    线程池的工作原理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.3" data-path="java_ftp.html">
            
                <a href="java_ftp.html">
            
                    
                    FTP文件服务器上传下载
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.4" data-path="collection.html">
            
                <a href="collection.html">
            
                    
                    集合
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.4.1" data-path="collection/java_collection_map.html">
            
                <a href="collection/java_collection_map.html">
            
                    
                    Map&HashMap&HashTable&ConcurrentHashMap
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.4.2" data-path="java_queue.html">
            
                <a href="java_queue.html">
            
                    
                    队列-queue
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.4.3" data-path="java_tree.html">
            
                <a href="java_tree.html">
            
                    
                    二叉树遍历
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    
        
        <li class="divider"></li>
        
        
    
        <li class="chapter active" data-level="3.1" data-path="java_io_nio.html">
            
                <a href="java_io_nio.html">
            
                    
                    java输入输出流
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.2" data-path="java_exception.html">
            
                <a href="java_exception.html">
            
                    
                    异常exception
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.3" data-path="java_serialize.html">
            
                <a href="java_serialize.html">
            
                    
                    Java序列化
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.4" data-path="Java_annotation.html">
            
                <a href="Java_annotation.html">
            
                    
                    注解
            
                </a>
            

            
        </li>
    

    
        
        <li class="divider"></li>
        
        
    
        <li class="chapter " data-level="4.1" data-path="multi.html">
            
                <a href="multi.html">
            
                    
                    多线程
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="4.1.1" data-path="multithread/java_multi_thread_volatile_synchronized.html">
            
                <a href="multithread/java_multi_thread_volatile_synchronized.html">
            
                    
                    volatile&synchronized
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.1.2" data-path="multithread/java_multi_thread_object_wait_notify.html">
            
                <a href="multithread/java_multi_thread_object_wait_notify.html">
            
                    
                    object/wait/notify
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.1.3" data-path="multithread/java_multi_thread_thread.html">
            
                <a href="multithread/java_multi_thread_thread.html">
            
                    
                    thread
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.1.4" data-path="multithread/java_multi_thread_threadpool.html">
            
                <a href="multithread/java_multi_thread_threadpool.html">
            
                    
                    线程池的工作原理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.1.5" data-path="multithread/java_multi_thread_lock.html">
            
                <a href="multithread/java_multi_thread_lock.html">
            
                    
                    lock&ReentrantLock&ReadWriteLock
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.1.6" data-path="multithread/java_multi_thread_lock_condition.html">
            
                <a href="multithread/java_multi_thread_lock_condition.html">
            
                    
                    lock&condition
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.1.7" data-path="multithread/java_multi_thread_tools.html">
            
                <a href="multithread/java_multi_thread_tools.html">
            
                    
                    Java并发工具- CountDownLatch&CyclicBarrier 
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.1.8" data-path="multithread/java_multi_thread_tools2.html">
            
                <a href="multithread/java_multi_thread_tools2.html">
            
                    
                    Java同步工具类- CountDownLatch&CyclicBarrier&Semaphore  
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    
        
        <li class="divider"></li>
        
        
    
        <li class="chapter " data-level="5.1" data-path="java_jvm.html">
            
                <a href="java_jvm.html">
            
                    
                    java虚拟机-JVM
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.2" data-path="java_java7new.html">
            
                <a href="java_java7new.html">
            
                    
                    java7新知识- Switch&TWR&Objects 
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.3" data-path="java_stream.html">
            
                <a href="java_stream.html">
            
                    
                    Java8对集合流操作stream
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.4" data-path="java_stream_groupby.html">
            
                <a href="java_stream_groupby.html">
            
                    
                    java8新语法-Stream-分组groupingBy操作 
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.5" data-path="java_optional.html">
            
                <a href="java_optional.html">
            
                    
                    java8新语法-Optional 
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.6" data-path="java_8new_date_api.html">
            
                <a href="java_8new_date_api.html">
            
                    
                    java8-新的日期时间API 
            
                </a>
            

            
        </li>
    

    
        
        <li class="divider"></li>
        
        
    
        <li class="chapter " data-level="6.1" data-path="../xml/xml_translation.html">
            
                <a href="../xml/xml_translation.html">
            
                    
                    XML中特殊符号的转译 
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="6.2" data-path="../datastructure/binary_complement.html">
            
                <a href="../datastructure/binary_complement.html">
            
                    
                    原码、反码、补码 
            
                </a>
            

            
        </li>
    

    
        
        <li class="divider"></li>
        
        
    
        <li class="chapter " data-level="7.1" data-path="basic/javabean2map.html">
            
                <a href="basic/javabean2map.html">
            
                    
                    Java bean转Map 
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">【第二章】算法积累题</li>
        
        
    
        <li class="chapter " data-level="8.1" data-path="../algorithm/merge_sorted_array.html">
            
                <a href="../algorithm/merge_sorted_array.html">
            
                    
                    算法题 - 合并排序数组 
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="8.2" data-path="../algorithm/lowest_score.html">
            
                <a href="../algorithm/lowest_score.html">
            
                    
                    算法题 - 最低分数线问题 
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="8.3" data-path="../algorithm/array_sub_list.html">
            
                <a href="../algorithm/array_sub_list.html">
            
                    
                    算法题 - 求数组的子集合 
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">【第三章】框架</li>
        
        
    
        <li class="chapter " data-level="9.1" data-path="../Spring/springboot.html">
            
                <a href="../Spring/springboot.html">
            
                    
                    SpringBoot
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="9.2" data-path="../Spring/spring_security.html">
            
                <a href="../Spring/spring_security.html">
            
                    
                    SpringSecurity
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="9.3" data-path="../linux/linux_commands_springboot_startup.html">
            
                <a href="../linux/linux_commands_springboot_startup.html">
            
                    
                    SpringBoot项目启动脚本
            
                </a>
            

            
        </li>
    

    
        
        <li class="divider"></li>
        
        
    
        <li class="chapter " data-level="10.1" data-path="../Spring/spring/spring_interview_questions_68.html">
            
                <a href="../Spring/spring/spring_interview_questions_68.html">
            
                    
                    Spring面试题68
            
                </a>
            

            
        </li>
    

    
        
        <li class="divider"></li>
        
        
    
        <li class="chapter " data-level="11.1" data-path="../Spring/springboot_auto_config.html">
            
                <a href="../Spring/springboot_auto_config.html">
            
                    
                    SpringBoot自动装载机制 - 源码分析
            
                </a>
            

            
        </li>
    

    
        
        <li class="divider"></li>
        
        
    
        <li class="chapter " data-level="12.1" data-path="../Spring/spring_feign.html">
            
                <a href="../Spring/spring_feign.html">
            
                    
                    Feign在spring、springboot下使用
            
                </a>
            

            
        </li>
    

    
        
        <li class="divider"></li>
        
        
    
        <li class="chapter " data-level="13.1" data-path="../Spring/spring_freemarker.html">
            
                <a href="../Spring/spring_freemarker.html">
            
                    
                    Spring boot集成freemarker，生成word文件
            
                </a>
            

            
        </li>
    

    
        
        <li class="divider"></li>
        
        
    
        <li class="chapter " data-level="14.1" data-path="../Spring/spring_freemarker_generate.html">
            
                <a href="../Spring/spring_freemarker_generate.html">
            
                    
                    Freemarker自动生成Java bean代码
            
                </a>
            

            
        </li>
    

    
        
        <li class="divider"></li>
        
        
    
        <li class="chapter " data-level="15.1" data-path="../mybatis/mybatis_metadata.html">
            
                <a href="../mybatis/mybatis_metadata.html">
            
                    
                    mybatis中操作元数据
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="15.2" data-path="../mybatis/mybatis_batch_exec_sql.html">
            
                <a href="../mybatis/mybatis_batch_exec_sql.html">
            
                    
                    mybatis中insert/update/delete标签中执行多条SQL语句
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="15.3" data-path="../mybatis/mybatis_plus.html">
            
                <a href="../mybatis/mybatis_plus.html">
            
                    
                    springboot集成mybatis-plus框架
            
                </a>
            

            
        </li>
    

    
        
        <li class="divider"></li>
        
        
    
        <li class="chapter " data-level="16.1" data-path="../distributed/sharding-jdbc.html">
            
                <a href="../distributed/sharding-jdbc.html">
            
                    
                    【分布式】sharding-jdbc实现分库分表
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">【第四章】数据库</li>
        
        
    
        <li class="chapter " data-level="17.1" data-path="../SQL/sql.html">
            
                <a href="../SQL/sql.html">
            
                    
                    SQL常用使用
            
                </a>
            

            
        </li>
    

    
        
        <li class="divider"></li>
        
        
    
        <li class="chapter " data-level="18.1" data-path="../redis/redis_study_notes.html">
            
                <a href="../redis/redis_study_notes.html">
            
                    
                    redis学习笔记
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="18.2" data-path="../redis/redis_list_second_kill.html">
            
                <a href="../redis/redis_list_second_kill.html">
            
                    
                    Redis中的队列list实现秒杀活动抢购
            
                </a>
            

            
        </li>
    

    
        
        <li class="divider"></li>
        
        
    
        <li class="chapter " data-level="19.1" data-path="../SQL/sql_mysql_green_install.html">
            
                <a href="../SQL/sql_mysql_green_install.html">
            
                    
                    mysql绿色版安装服务教程
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="19.2" data-path="../SQL/sql_h2.html">
            
                <a href="../SQL/sql_h2.html">
            
                    
                    H2内嵌数据的使用
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="19.3" data-path="../SQL/cache.html">
            
                <a href="../SQL/cache.html">
            
                    
                    缓存
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="19.4" data-path="../SQL/mysql/mysql_back_rollback.html">
            
                <a href="../SQL/mysql/mysql_back_rollback.html">
            
                    
                    MySQL单表备份还原
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">【第五章】Web</li>
        
        
    
        <li class="chapter " data-level="20.1" data-path="../web/web_js_jquery.html">
            
                <a href="../web/web_js_jquery.html">
            
                    
                    web简单积累
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">【第六章】Linux</li>
        
        
    
        <li class="chapter " data-level="21.1" data-path="../linux/linux_common_commands.html">
            
                <a href="../linux/linux_common_commands.html">
            
                    
                    linux常用命令
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">【第七章】Windows</li>
        
        
    
        <li class="chapter " data-level="22.1" data-path="../windows/windows_common_commands.html">
            
                <a href="../windows/windows_common_commands.html">
            
                    
                    windows常用命令
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">【第八章】错误集</li>
        
        
    
        <li class="chapter " data-level="23.1" data-path="../error/maven_not_found_local_jar.html">
            
                <a href="../error/maven_not_found_local_jar.html">
            
                    
                    maven引入依赖时识别不到本地仓库的jar
            
                </a>
            

            
        </li>
    

    
        
        <li class="divider"></li>
        
        
    
        <li class="chapter " data-level="24.1" data-path="../error/security_not_configure_antMatchers_after_anyRequest.html">
            
                <a href="../error/security_not_configure_antMatchers_after_anyRequest.html">
            
                    
                    spring security 配置发生错误 Can't configure antMatchers after anyRequest
            
                </a>
            

            
        </li>
    

    
        
        <li class="divider"></li>
        
        
    
        <li class="chapter " data-level="25.1" data-path="../error/AOP_stream_close_error.html">
            
                <a href="../error/AOP_stream_close_error.html">
            
                    
                    AOP内获取了对请求流操作后报流已经关闭错误的问题
            
                </a>
            

            
        </li>
    

    
        
        <li class="divider"></li>
        
        
    
        <li class="chapter " data-level="26.1" data-path="../tools/github/github_ssh.html">
            
                <a href="../tools/github/github_ssh.html">
            
                    
                    Github ssh key 解决提交后push频繁输入用户名和密码
            
                </a>
            

            
        </li>
    

    
        
        <li class="divider"></li>
        
        
    
        <li class="chapter " data-level="27.1" data-path="../error/sharding-jdbc-error-datasources.html">
            
                <a href="../error/sharding-jdbc-error-datasources.html">
            
                    
                    sharding-jdbc-4.1.1版本启动数据源报错问题
            
                </a>
            

            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            Published with GitBook
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href=".." >java输入输出流</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
                                <section class="normal markdown-section">
                                
                                <div id="anchor-navigation-ex-navbar"><i class="fa fa-navicon"></i><ul><ul><ul><li><span class="title-icon "></span><a href="#io"><b></b>IO</a></li><li><span class="title-icon "></span><a href="#nio2"><b></b>NIO.2</a></li></ul></ul></ul></div><h3 id="io"><a name="io" class="anchor-navigation-ex-anchor" href="#io"><i class="fa fa-link" aria-hidden="true"></i></a>IO</h3>
<ul>
<li>&#x5B57;&#x8282;&#x6D41;&#xFF1A;InputStream&#x3001;OutputStream   </li>
<li>&#x5B57;&#x7B26;&#x6D41;&#xFF1A;InputStreamReader&#x3001;OutputStreamWriter   </li>
</ul>
<h4 id="&#x7F13;&#x51B2;&#x533A;"><a name="&#x7F13;&#x51B2;&#x533A;" class="anchor-navigation-ex-anchor" href="#&#x7F13;&#x51B2;&#x533A;"><i class="fa fa-link" aria-hidden="true"></i></a><strong>&#x7F13;&#x51B2;&#x533A;</strong></h4>
<p>&#x200B;    <strong>&#x7F13;&#x51B2;&#x533A;</strong>&#x5728;&#x67D0;&#x4E9B;&#x7279;&#x6027;&#x4E0B;&#x7C7B;&#x4F3C;&#x4E0E;java&#x4E2D;&#x7684;&#x57FA;&#x672C;&#x7C7B;&#x578B;&#x6570;&#x7EC4;(&#x5982;&#x5B57;&#x8282;&#x6570;&#x7EC4;&#x7B49;)&#xFF0C;&#x7F13;&#x5B58;&#x533A;&#x4E2D;&#x7684;&#x6570;&#x636E;&#x6392;&#x5217;&#x662F;&#x7EBF;&#x6027;&#x7684;&#xFF0C;&#x7A7A;&#x95F4;&#x4E5F;&#x662F;&#x6709;&#x9650;&#x7684;&#x3002;</p>
<p>&#x200B;    <strong>&#x7F13;&#x51B2;&#x533A;</strong>&#x7684;3&#x4E2A;&#x72B6;&#x6001;&#x53D8;&#x91CF;&#xFF1A;<strong>&#x5BB9;&#x5668;</strong>(capacity)&#x3001;<strong>&#x8BFB;&#x5199;&#x9650;&#x5236;</strong>(limit)&#x548C;<strong>&#x8BFB;&#x5199;&#x4F4D;&#x7F6E;</strong>(position)&#x3002;   </p>
<p>&#x200B;    <strong>&#x5BB9;&#x91CF;:</strong> &#x6307;&#x7684;&#x662F;&#x7F13;&#x51B2;&#x533A;&#x7684;&#x989D;&#x5B9A;&#x5927;&#x5C0F;&#xFF0C;&#x5728;&#x521B;&#x5EFA;&#x7F13;&#x51B2;&#x533A;&#x65F6;&#x6307;&#x5B9A;&#x7684;&#xFF0C;&#x65E0;&#x6CD5;&#x5728;&#x521B;&#x5EFA;&#x540E;&#x66F4;&#x6539;&#x3002;&#x4EFB;&#x4F55;&#x65F6;&#x523B;&#xFF0C;&#x7F13;&#x51B2;&#x533A;&#x7684;&#x6570;&#x636E;&#x603B;&#x6570;&#x90FD;&#x4E0D;&#x53EF;&#x8D85;&#x8FC7;&#x5BB9;&#x91CF;&#x3002;  </p>
<p>&#x200B;    <strong>&#x8BFB;&#x5199;&#x9650;&#x5236;:</strong> &#x8868;&#x793A;&#x662F;&#x5728;&#x7F13;&#x51B2;&#x533A;&#x4E2D;&#x8FDB;&#x884C;&#x8BFB;&#x5199;&#x64CD;&#x4F5C;&#x65F6;&#x7684;&#x6700;&#x5927;&#x5141;&#x8BB8;&#x4F4D;&#x7F6E;&#x3002;&#x5982;&#x5BF9;&#x4E8E;&#x4E00;&#x4E2A;&#x5BB9;&#x91CF;&#x4E3A;32&#x7684;&#x7F13;&#x51B2;&#x533A;&#xFF0C;&#x5982;&#x679C;&#x8BFB;&#x5199;&#x9650;&#x5236;&#x662F;16&#xFF0C;&#x5219;&#x53EA;&#x6709;&#x524D;&#x534A;&#x4E2A;&#x7F13;&#x51B2;&#x533A;&#x5728;&#x8BFB;&#x5199;&#x65F6;&#x53EF;&#x7528;&#x3002;&#x5982;&#x679C;&#x5E0C;&#x671B;&#x540E;&#x534A;&#x4E2A;&#x7F13;&#x51B2;&#x533A;&#x53EF;&#x7528;&#xFF0C;&#x5219;&#x5FC5;&#x987B;&#x628A;&#x8BFB;&#x5199;&#x9650;&#x5236;&#x6539;&#x4E3A;32&#x3002;   </p>
<p>&#x200B;    <strong>&#x8BFB;&#x5199;&#x4F4D;&#x7F6E;:</strong> &#x8868;&#x793A;&#x5F53;&#x524D;&#x8BFB;&#x5199;&#x64CD;&#x4F5C;&#x65F6;&#x7684;&#x4F4D;&#x7F6E;&#x3002;   </p>
<p>&#x200B;    &#x5728;&#x4EFB;&#x4F55;&#x65F6;&#x5019;&#xFF0C;&#x7F13;&#x51B2;&#x533A;&#x4E2D;&#x7684;&#x5404;&#x4E2A;&#x72B6;&#x6001;&#x53D8;&#x91CF;&#x4E4B;&#x95F4;&#x6EE1;&#x8DB3;&#x5173;&#x7CFB;&#xFF1A;<strong>0&lt;=&#x6807;&#x8BB0;&#x4F4D;&#x7F6E;&lt;=&#x8BFB;&#x5199;&#x4F4D;&#x7F6E;&lt;=&#x8BFB;&#x5199;&#x9650;&#x5236;&lt;=&#x5BB9;&#x5668;</strong>   </p>
<p>&#x200B;    Java   NIO&#x4E2D;&#x7684;java.nio.<strong>Buffer</strong>&#x7C7B;&#x662F;&#x6240;&#x6709;&#x4E0D;&#x540C;&#x6570;&#x636E;&#x7C7B;&#x578B;&#x7684;&#x7F13;&#x51B2;&#x533A;&#x7684;&#x7236;&#x7C7B;&#x3002;   </p>
<p>&#x200B;    &#x4E00;&#x4E9B;&#x5E38;&#x7528;&#x65B9;&#x6CD5;&#xFF1A;  </p>
<p>&#x200B;        &#x5411;&#x7F13;&#x51B2;&#x533A;<strong>&#x5199;&#x5165;</strong>&#x6570;&#x636E;&#xFF0C;&#x8C03;&#x7528;<strong>clear</strong>&#x65B9;&#x6CD5;&#xFF0C;&#x5219;&#x4F1A;&#x4FEE;&#x6539;&#xFF1A;&#x8BFB;&#x5199;&#x9650;&#x5236;=&#x5BB9;&#x5668;&#xFF0C;&#x8BFB;&#x5199;&#x4F4D;&#x7F6E;=0   </p>
<p>&#x200B;        &#x5411;&#x7F13;&#x51B2;&#x533A;<strong>&#x8BFB;&#x53D6;</strong>&#x6570;&#x636E;&#xFF0C;&#x8C03;&#x7528;<strong>flip</strong>&#x65B9;&#x6CD5;&#xFF0C;&#x5219;&#x4F1A;&#x4FEE;&#x6539;&#xFF1A;&#x8BFB;&#x5199;&#x9650;&#x5236;=&#x8BFB;&#x5199;&#x4F4D;&#x7F6E;&#xFF0C;&#x8BFB;&#x5199;&#x4F4D;&#x7F6E;=0   </p>
<p>&#x200B;        <strong>&#x91CD;&#x65B0;&#x8BFB;&#x53D6;</strong>&#x7F13;&#x51B2;&#x533A;&#x6570;&#x636E;&#xFF0C;&#x8C03;&#x7528;<strong>rewind</strong>&#x65B9;&#x6CD5;&#xFF0C;&#x5219;&#x4F1A;&#x4FEE;&#x6539;&#xFF1A;&#x8BFB;&#x5199;&#x4F4D;&#x7F6E;=0&#xFF0C;&#x4E0D;&#x4F1A;&#x4FEE;&#x6539;&#x8BFB;&#x5199;&#x9650;&#x5236;      </p>
<p>&#x200B;        &#x6700;&#x91CD;&#x8981;&#x7684;&#x5B9E;&#x73B0;&#x7C7B;java.nio.<strong>ByteBuffer</strong>&#x3002;   </p>
<p>&#x4EE3;&#x7801;</p>
<pre><code class="lang-java"><span class="hljs-meta">@Test</span>
<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">buffer</span><span class="hljs-params">()</span></span>{

    ByteBuffer buffer = ByteBuffer.allocate(<span class="hljs-number">4</span>);
    buffer.putInt(<span class="hljs-number">12</span>); <span class="hljs-comment">// int &#x5360;&#x7528;4&#x4E2A;&#x5B57;&#x8282;</span>
<span class="hljs-comment">//        buffer.putChar(&apos;A&apos;); // &#x518D;&#x5B58;&#x653E;&#x5C31;&#x7F13;&#x5B58;&#x6EA2;&#x51FA; BufferOverflowException</span>

    buffer.flip(); <span class="hljs-comment">//&#x8BFB;&#x53D6;&#x7F13;&#x51B2;&#x533A;&#x65F6;&#x8C03;&#x7528;</span>

    <span class="hljs-keyword">int</span> xx = buffer.getInt();
    System.out.println(xx);
}
</code></pre>
<h4 id="&#x901A;&#x9053;"><a name="&#x901A;&#x9053;" class="anchor-navigation-ex-anchor" href="#&#x901A;&#x9053;"><i class="fa fa-link" aria-hidden="true"></i></a><strong>&#x901A;&#x9053;</strong></h4>
<p>&#x200B;    <strong>&#x901A;&#x9053;</strong>&#x662F;java NIO&#x5BF9;I/O&#x64CD;&#x4F5C;&#x63D0;&#x4F9B;&#x7684;&#x4E00;&#x79CD;&#x65B0;&#x7684;&#x62BD;&#x8C61;&#x65B9;&#x5F0F;&#x3002;&#x8868;&#x793A;&#x4E3A;&#x4E86;&#x4E00;&#x4E2A;&#x5DF2;&#x7ECF;&#x5EFA;&#x7ACB;&#x597D;&#x7684;&#x5230;&#x652F;&#x6301;I/O&#x64CD;&#x4F5C;&#x7684;&#x5B9E;&#x4F53;&#x7684;&#x8FDE;&#x63A5;&#x3002;&#x8FDE;&#x63A5;&#x4E00;&#x65E6;&#x5EFA;&#x7ACB;&#xFF0C;&#x5C31;&#x53EF;&#x4EE5;&#x5728;&#x8FD9;&#x4E2A;&#x8FDE;&#x63A5;&#x4E0A;&#x8FDB;&#x884C;&#x5404;&#x79CD;I/O&#x64CD;&#x4F5C;&#x3002;</p>
<p>&#x200B;    &#x5728;&#x901A;&#x9053;&#x4E0A;&#x6267;&#x884C;&#x7684;&#x64CD;&#x4F5C;&#x7C7B;&#x578B;&#x53D6;&#x51B3;&#x4E8E;&#x901A;&#x9053;&#x7684;&#x7279;&#x6027;&#xFF0C;&#x4E00;&#x822C;&#x7684;&#x64CD;&#x4F5C;&#x5305;&#x62EC;&#x6570;&#x636E;&#x7684;&#x8BFB;&#x53D6;&#x548C;&#x5199;&#x5165;&#x3002;   </p>
<p>&#x200B;    Java NIO&#x4E2D;&#x7684;&#x901A;&#x9053;&#x90FD;&#x5B9E;&#x73B0;&#x4E86;java.nio.channels.<strong>Channel</strong>&#x63A5;&#x53E3;&#x3002;   </p>
<h5 id="&#x6587;&#x4EF6;&#x901A;&#x9053;"><a name="&#x6587;&#x4EF6;&#x901A;&#x9053;" class="anchor-navigation-ex-anchor" href="#&#x6587;&#x4EF6;&#x901A;&#x9053;"><i class="fa fa-link" aria-hidden="true"></i></a><strong>&#x6587;&#x4EF6;&#x901A;&#x9053;</strong></h5>
<p>   &#x6587;&#x4EF6;&#x662F;I/O&#x64CD;&#x4F5C;&#x7684;&#x4E00;&#x4E2A;&#x5E38;&#x89C1;&#x5B9E;&#x4F53;&#x3002;&#x4E0E;&#x6587;&#x4EF6;&#x5B9E;&#x4F53;&#x5BF9;&#x5E94;&#x7684;&#x8868;&#x793A;&#x6587;&#x4EF6;&#x901A;&#x9053;&#x7684;java.nio.channels.<strong>FileChannel</strong>&#x4E5F;&#x662F;&#x4E00;&#x4E2A;&#x5F3A;&#x5927;&#x7684;&#x901A;&#x9053;&#x5B9E;&#x73B0;&#x3002;   </p>
<p><strong>&#x5199;&#x5165;&#x6587;&#x4EF6;&#x901A;&#x9053;</strong></p>
<pre><code class="lang-java">    <span class="hljs-meta">@Test</span>
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">fileChannel</span><span class="hljs-params">()</span></span>{
        <span class="hljs-comment">// &#x5B9A;&#x4E49; &#x6587;&#x4EF6;&#x901A;&#x9053; &#xFF0C;&#x5C5E;&#x6027;&#xFF1A;create&#x6CA1;&#x6709;&#x6587;&#x4EF6;&#x5219;&#x65B0;&#x5EFA;&#xFF0C;append&#x6587;&#x4EF6;&#x672B;&#x8FFD;&#x52A0;&#xFF0C;write&#x53EF;&#x5199;</span>
        <span class="hljs-keyword">try</span>(FileChannel channel = FileChannel.open(Paths.get(<span class="hljs-string">&quot;F:\\demo\\a.txt&quot;</span>),
                StandardOpenOption.CREATE,
                  StandardOpenOption.APPEND,
                StandardOpenOption.WRITE);){
            <span class="hljs-comment">// &#x5B9A;&#x4E49;&#x7F13;&#x51B2;&#x533A;</span>
            ByteBuffer buffer =    ByteBuffer.allocate(<span class="hljs-number">64</span>)
                .putChar(<span class="hljs-string">&apos;A&apos;</span>)
                .putChar(<span class="hljs-string">&apos;G&apos;</span>)
                .putChar(<span class="hljs-string">&apos;E&apos;</span>);
            buffer.flip(); <span class="hljs-comment">// &#x8BFB;&#x53D6;&#x7F13;&#x51B2;&#x533A;&#x65F6;&#x8C03;&#x7528;</span>
            channel.write(buffer); <span class="hljs-comment">// &#x5199;&#x5165;&#x6587;&#x4EF6;&#x901A;&#x9053;&#x4E2D;</span>
        } <span class="hljs-keyword">catch</span> (Exception e) {
            e.printStackTrace();
        }
    }
</code></pre>
<p><strong>&#x6587;&#x4EF6;&#x901A;&#x9053;&#x5C5E;&#x6027;&#x9009;&#x9879;</strong></p>
<p>&#x200B;    <strong>StandardOpenOption</strong>&#xFF1A;   </p>
<p>&#x200B;        Read&#xFF1A;&#x53EF;&#x8BFB;  </p>
<p>&#x200B;        Write&#xFF1A;&#x53EF;&#x5199;   </p>
<p>&#x200B;        Append&#xFF1A;&#x6587;&#x4EF6;&#x672B;&#x8FFD;&#x52A0;   </p>
<p>&#x200B;        Create&#xFF1A;&#x76EE;&#x6807;&#x6587;&#x4EF6;&#x4E0D;&#x5B58;&#x5728;&#x65F6;&#xFF0C;&#x521B;&#x5EFA;&#x65B0;&#x6587;&#x4EF6;   </p>
<p>&#x200B;        Create_new&#xFF1A;&#x4E0D;&#x5B58;&#x5728;&#xFF0C;&#x5219;&#x521B;&#x5EFA;&#xFF0C;&#x5B58;&#x5728;&#x5219;&#x62A5;&#x9519;   </p>
<p>&#x200B;        Delete_on_close:&#x521B;&#x5EFA;&#x4E34;&#x65F6;&#x6587;&#x4EF6;&#xFF0C;&#x901A;&#x9053;&#x5173;&#x95ED;&#x662F;&#xFF0C;JVM&#x4F1A;&#x5C1D;&#x8BD5;&#x5220;&#x9664;&#x8BE5;&#x6587;&#x4EF6;   </p>
<p><strong>&#x6587;&#x4EF6;&#x6570;&#x636E;&#x4F20;&#x8F93;</strong></p>
<p>&#x200B;    FileChannel&#x7C7B;&#x63D0;&#x4F9B;&#x4E86;<strong>transferFrom</strong>&#x548C;<strong>transferTo</strong>&#x65B9;&#x6CD5;&#x7528;&#x4E86;&#x5FEB;&#x901F;&#x5730;&#x4F20;&#x8F93;&#x6570;&#x636E;&#x3002;   </p>
<p>&#x200B;    <strong>transferFrom</strong>&#x65B9;&#x6CD5;&#x628A;&#x6765;&#x81EA;&#x4E00;&#x4E2A;&#x5B9E;&#x73B0;&#x4E86;<strong>ReadableByteChannel</strong>&#x63A5;&#x53E3;&#x7684;&#x901A;&#x9053;&#x4E2D;&#x7684;&#x6570;&#x636E;&#x5199;&#x5165;&#x6587;&#x4EF6;&#x901A;&#x9053;&#x4E2D;&#x3002;   </p>
<p>&#x200B;    <strong>transferTo</strong>&#x65B9;&#x6CD5;&#x628A;&#x5F53;&#x524D;&#x6587;&#x4EF6;&#x901A;&#x9053;&#x4E2D;&#x7684;&#x6570;&#x636E;&#x4F20;&#x8F93;&#x5230;&#x4E00;&#x4E2A;&#x5B9E;&#x73B0;&#x4E86;<strong>WriteableByteChannel</strong>&#x63A5;&#x53E3;&#x7684;&#x901A;&#x9053;&#x4E2D;&#x3002;</p>
<pre><code class="lang-java">    <span class="hljs-meta">@Test</span>
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">transfer</span><span class="hljs-params">()</span></span>{
        <span class="hljs-comment">// &#x5B9A;&#x4E49; &#x6E90;&#x6587;&#x4EF6;&#x901A;&#x9053; &#x548C; &#x76EE;&#x6807;&#x6587;&#x4EF6;&#x901A;&#x9053;</span>
        <span class="hljs-keyword">try</span>(
            FileChannel src = FileChannel.open(Paths.get(<span class="hljs-string">&quot;F:\\demo\\a.txt&quot;</span>),StandardOpenOption.READ);
           FileChannel dest = FileChannel.open(Paths.get(<span class="hljs-string">&quot;F:\\demo\\dest.txt&quot;</span>), 
                        StandardOpenOption.CREATE,
                        StandardOpenOption.WRITE,
                           StandardOpenOption.APPEND);){
            <span class="hljs-comment">// &#x4F20;&#x8F93;&#x6570;&#x636E;&#x5230; &#x76EE;&#x6807;&#x6587;&#x4EF6;&#x901A;&#x9053;&#x4E2D;</span>
            src.transferTo(<span class="hljs-number">0</span>, src.size(), dest);
            <span class="hljs-comment">// &#x4ECE;&#x6E90;&#x6587;&#x4EF6;&#x901A;&#x9053;&#x4E2D;&#x83B7;&#x53D6;&#x6570;&#x636E; &#x5199;&#x5165;&#x6587;&#x4EF6;</span>
            dest.transferFrom(src, <span class="hljs-number">0</span>, Integer.MAX_VALUE);
        } <span class="hljs-keyword">catch</span> (Exception e) {
            e.printStackTrace();
        }
    }
</code></pre>
<h5 id="&#x5185;&#x5B58;&#x6620;&#x5C04;&#x6587;&#x4EF6;"><a name="&#x5185;&#x5B58;&#x6620;&#x5C04;&#x6587;&#x4EF6;" class="anchor-navigation-ex-anchor" href="#&#x5185;&#x5B58;&#x6620;&#x5C04;&#x6587;&#x4EF6;"><i class="fa fa-link" aria-hidden="true"></i></a><strong>&#x5185;&#x5B58;&#x6620;&#x5C04;&#x6587;&#x4EF6;</strong></h5>
<p>   &#x901A;&#x8FC7;&#x64CD;&#x4F5C;&#x7CFB;&#x7EDF;&#x7684;&#x5185;&#x5B58;&#x6620;&#x5C04;&#x6587;&#x4EF6;&#x652F;&#x6301;&#xFF0C;&#x53EF;&#x4EE5;&#x6BD4;&#x8F83;&#x5FEB;&#x901F;&#x5730;&#x5BF9;&#x5927;&#x6587;&#x4EF6;&#x8FDB;&#x884C;&#x64CD;&#x4F5C;&#x3002;</p>
<p>&#x200B;    &#x5185;&#x5B58;&#x6620;&#x5C04;&#x6587;&#x4EF6;&#x7684;<strong>&#x539F;&#x7406;</strong>&#x5728;&#x4E8E;&#x628A;&#x7CFB;&#x7EDF;&#x5185;&#x5B58;&#x5730;&#x5740;&#x6620;&#x5C04;&#x5230;&#x64CD;&#x4F5C;&#x7684;&#x6587;&#x4EF6;&#x4E0A;&#x3002;&#x8BFB;&#x53D6;&#x8FD9;&#x4E9B;&#x5185;&#x5B58;&#x5730;&#x5740;&#x5C31;&#x76F8;&#x5F53;&#x4E8E;&#x8BFB;&#x53D6;&#x6587;&#x4EF6;&#x7684;&#x5185;&#x5BB9;&#xFF0C;&#x800C;&#x6539;&#x53D8;&#x8FD9;&#x4E9B;&#x5185;&#x5B58;&#x5730;&#x5740;&#x7684;&#x503C;&#x5C31;&#x76F8;&#x5F53;&#x4E8E;&#x4FEE;&#x6539;&#x6587;&#x4EF6;&#x4E2D;&#x7684;&#x5185;&#x5BB9;&#x3002;&#x901A;&#x8FC7;&#x5185;&#x5B58;&#x6620;&#x5C04;&#x7684;&#x65B9;&#x5F0F;&#x5BF9;&#x6587;&#x4EF6;&#x8FDB;&#x884C;&#x64CD;&#x4F5C;&#x65F6;&#xFF0C;&#x4E0D;&#x518D;&#x9700;&#x8981;&#x901A;&#x8FC7;I/O&#x64CD;&#x4F5C;&#x6765;&#x5B8C;&#x6210;&#xFF0C;&#x800C;&#x662F;&#x76F4;&#x63A5;&#x901A;&#x8FC7;&#x5185;&#x5B58;&#x5730;&#x5740;&#x8BBF;&#x95EE;&#x64CD;&#x4F5C;&#x6765;&#x5B8C;&#x6210;&#xFF0C;&#x8FD9;&#x5C31;&#x5927;&#x5927;&#x63D0;&#x9AD8;&#x4E86;&#x64CD;&#x4F5C;&#x6587;&#x4EF6;&#x7684;&#x6027;&#x80FD;&#xFF0C;&#x56E0;&#x4E3A;I/O&#x64CD;&#x4F5C;&#x6BD4;&#x8BBF;&#x95EE;&#x5185;&#x5B58;&#x5730;&#x5740;&#x8981;&#x6162;&#x5F97;&#x591A;&#x3002;       </p>
<p><strong>FileChannel</strong>&#x7C7B;&#x7684;map&#x65B9;&#x6CD5;&#x53EF;&#x4EE5;&#x628A;&#x4E00;&#x4E2A;&#x6587;&#x4EF6;&#x7684;&#x5168;&#x90E8;&#x6216;&#x90E8;&#x5206;&#x5185;&#x5BB9;&#x6620;&#x5C04;&#x5230;&#x5185;&#x5B58;&#x4E2D;&#xFF0C;&#x6240;&#x5F97;&#x5230;&#x7684;&#x662F;&#x4E00;&#x4E2A;ByteBuffer&#x7C7B;&#x7684;&#x5B50;&#x7C7B;<strong>MappedByteBuffer</strong>&#x7684;&#x5BF9;&#x8C61;&#xFF0C;&#x7A0B;&#x5E8F;&#x53EA;&#x9700;&#x8981;&#x5BF9;&#x8FD9;&#x4E2A;<strong>MappedByteBuffer</strong>&#x7C7B;&#x7684;&#x5BF9;&#x8C61;&#x8FDB;&#x884C;&#x64CD;&#x4F5C;&#x5373;&#x53EF;,&#x5BF9;&#x5176;&#x64CD;&#x4F5C;&#x4F1A;&#x81EA;&#x52A8;&#x540C;&#x6B65;&#x5230;&#x6587;&#x4EF6;&#x5185;&#x5BB9;&#x4E2D;&#x3002;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;<strong>force</strong>&#x65B9;&#x6CD5;&#x8FDB;&#x884C;&#x7ACB;&#x5373;&#x540C;&#x6B65;&#x5230;&#x6587;&#x4EF6;&#x4E2D;&#x3002;   </p>
<h6 id="&#x6620;&#x5C04;&#x6A21;&#x5F0F;"><a name="&#x6620;&#x5C04;&#x6A21;&#x5F0F;" class="anchor-navigation-ex-anchor" href="#&#x6620;&#x5C04;&#x6A21;&#x5F0F;"><i class="fa fa-link" aria-hidden="true"></i></a><strong>&#x6620;&#x5C04;&#x6A21;&#x5F0F;</strong></h6>
<p>   <strong>FileChannel.MapMode</strong>&#x8FD9;&#x4E2A;&#x679A;&#x4E3E;&#x7C7B;&#x578B;&#x8868;&#x793A;&#xFF1A;   </p>
<ul>
<li><strong>READ_ONLY</strong>:&#x53EA;&#x8BFB;&#x64CD;&#x4F5C;   </li>
<li><strong>READ_WRITE</strong>:&#x53EF;&#x8BFB;&#x53EF;&#x5199;   </li>
<li><strong>PRIVATE</strong>:&#x5BF9;MappedByteBuffer&#x6240;&#x4FEE;&#x6539;&#x4E0D;&#x4F1A;&#x540C;&#x6B65;&#x5230;&#x6587;&#x4EF6;&#x4E2D;&#xFF0C;&#x800C;&#x662F;&#x540C;&#x6B65;&#x5230;&#x4E00;&#x4E2A;&#x79C1;&#x6709;&#x7684;&#x526F;&#x672C;&#x4E2D;&#x3002;   </li>
</ul>
<pre><code class="lang-java">    <span class="hljs-meta">@Test</span>
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">mappedByteBuffer</span><span class="hljs-params">()</span></span>{
        <span class="hljs-comment">// &#x6587;&#x4EF6;&#x901A;&#x9053;</span>
        <span class="hljs-keyword">try</span>(FileChannel channel = FileChannel.open(Paths.get(<span class="hljs-string">&quot;F:\\demo\\a.txt&quot;</span>), 
                StandardOpenOption.CREATE,
                StandardOpenOption.READ,
                StandardOpenOption.WRITE);){
            <span class="hljs-comment">// &#x5185;&#x5B58;&#x6620;&#x5C04;&#x6587;&#x4EF6; READ_WRITE&#xFF1A;&#x53EF;&#x8BFB;&#x5199;</span>
            MappedByteBuffer buffer = channel.map(FileChannel.MapMode.READ_WRITE, <span class="hljs-number">0</span>, channel.size());
            <span class="hljs-keyword">byte</span> b = buffer.get();
            buffer.put(<span class="hljs-number">7</span>,b);
            <span class="hljs-comment">// &#x7ACB;&#x5373;&#x540C;&#x6B65;&#x5230;&#x6587;&#x4EF6;&#x4E2D;</span>
            buffer.force();
        } <span class="hljs-keyword">catch</span> (Exception e) {
            e.printStackTrace();
        }
    }
</code></pre>
<hr>
<h3 id="nio2"><a name="nio2" class="anchor-navigation-ex-anchor" href="#nio2"><i class="fa fa-link" aria-hidden="true"></i></a><strong>NIO.2</strong></h3>
<h4 id="path"><a name="path" class="anchor-navigation-ex-anchor" href="#path"><i class="fa fa-link" aria-hidden="true"></i></a><strong>Path</strong></h4>
<p>   <strong>Path</strong>&#x6587;&#x4EF6;&#x8DEF;&#x5F84;&#x7684;&#x62BD;&#x8C61;&#x3002;&#x63D0;&#x4F9B;&#x4E86;&#x64CD;&#x4F5C;&#x8DEF;&#x5F84;&#x7684;&#x5F88;&#x591A;&#x5B9E;&#x7528;&#x65B9;&#x6CD5;   </p>
<p>&#x200B;    resolve&#x65B9;&#x6CD5;&#xFF1A;&#x628A;&#x5F53;&#x524D;&#x8DEF;&#x5F84;&#x5F53;&#x6210;&#x7236;&#x76EE;&#x5F55;&#xFF0C;&#x628A;&#x53C2;&#x6570;&#x5F53;&#x6210;&#x5B50;&#x76EE;&#x5F55;&#xFF0C;&#x89E3;&#x6790;&#x6210;&#x4E00;&#x4E2A;&#x65B0;&#x76EE;&#x5F55;   </p>
<p>&#x200B;    resolveSibling&#x65B9;&#x6CD5;&#xFF1A;&#x8DDF;resolve&#x4E00;&#x6837;&#xFF0C;&#x53EA;&#x662F;&#x628A;&#x5F53;&#x524D;&#x8DEF;&#x5F84;&#x7684;&#x7236;&#x76EE;&#x5F55;&#x5F53;&#x6210;&#x89E3;&#x6790;&#x89E3;&#x6790;&#x65F6;&#x7684;&#x7236;&#x76EE;&#x5F55;   </p>
<p>&#x200B;    subpath&#x65B9;&#x6CD5;&#xFF1A;&#x83B7;&#x53D6;&#x5F53;&#x524D;&#x8DEF;&#x5F84;&#x7684;&#x5B50;&#x8DEF;&#x5F84;&#xFF0C;&#x53C2;&#x6570;&#x4E2D;&#x7684;&#x5E8F;&#x53F7;&#x8868;&#x793A;&#x7684;&#x662F;&#x8DEF;&#x5F84;&#x4E2D;&#x540D;&#x79F0;&#x5143;&#x7D20;&#x7684;&#x5E8F;&#x53F7;   </p>
<p>&#x200B;    startsWith&#x65B9;&#x6CD5;&#xFF1A;&#x5F53;&#x524D;&#x8DEF;&#x5F84;&#x662F;&#x5426;&#x4EE5;&#x53C2;&#x6570;&#x8DEF;&#x5F84;&#x5F00;&#x59CB;   </p>
<p>&#x200B;    endsWith&#x65B9;&#x6CD5;&#xFF1A;&#x5F53;&#x524D;&#x8DEF;&#x5F84;&#x662F;&#x5426;&#x4EE5;&#x53C2;&#x6570;&#x8DEF;&#x5F84;&#x7ED3;&#x5C3E;   </p>
<p>&#x200B;    normalize&#x65B9;&#x6CD5;&#xFF1A;&#x83B7;&#x53D6;&#x5230;&#x771F;&#x5B9E;&#x4F4D;&#x7F6E;&#xFF0C;&#x53BB;&#x6389;&#x4E00;&#x4E9B;./,../&#x7B49;&#x5197;&#x4F59;&#x8DEF;&#x5F84;   </p>
<pre><code class="lang-java">    <span class="hljs-meta">@Test</span>
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">path</span><span class="hljs-params">()</span></span>{
        Path path1 = Paths.get(<span class="hljs-string">&quot;a&quot;</span>,<span class="hljs-string">&quot;b&quot;</span>); <span class="hljs-comment">// a\b</span>
        Path path2 = Paths.get(<span class="hljs-string">&quot;c&quot;</span>,<span class="hljs-string">&quot;d&quot;</span>); <span class="hljs-comment">// c\d</span>

        Path res = path1.resolve(path2); <span class="hljs-comment">// a\b\c\d</span>
        Path resv = path1.resolveSibling(path2); <span class="hljs-comment">// a\c\d</span>

        Path subpath = path1.subpath(<span class="hljs-number">0</span>, <span class="hljs-number">1</span>); <span class="hljs-comment">// a</span>
        <span class="hljs-keyword">boolean</span> startsWith = path1.startsWith(<span class="hljs-string">&quot;a&quot;</span>); <span class="hljs-comment">// true</span>

        Path normalize = Paths.get(<span class="hljs-string">&quot;./a/../a/b/../c/d&quot;</span>).normalize(); <span class="hljs-comment">// a\c\d</span>
    }
</code></pre>
<h4 id="files"><a name="files" class="anchor-navigation-ex-anchor" href="#files"><i class="fa fa-link" aria-hidden="true"></i></a><strong>Files</strong></h4>
<p>   <strong>Files</strong>&#x7B80;&#x5316;&#x6587;&#x4EF6;&#x64CD;&#x4F5C;&#x7684;&#x5DE5;&#x5177;&#x7C7B;&#x3002;  </p>
<p>&#x200B;     Files.<strong>createFile</strong>&#x521B;&#x5EFA;&#x6587;&#x4EF6;   </p>
<p>&#x200B;    Files.<strong>delete</strong>&#x5220;&#x9664;&#x6587;&#x4EF6;   </p>
<p>&#x200B;    Files.<strong>copy</strong>(source,target)&#x590D;&#x5236;&#x6587;&#x4EF6;   </p>
<p>&#x200B;    Files.<strong>move</strong>(source,target)&#x79FB;&#x52A8;   </p>
<pre><code class="lang-java"><span class="hljs-meta">@Test</span>
<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">files</span><span class="hljs-params">()</span> <span class="hljs-keyword">throws</span> IOException</span>{
    <span class="hljs-comment">// &#x521B;&#x5EFA;&#x6587;&#x4EF6;</span>
    Path path = Files.createFile(Paths.get(<span class="hljs-string">&quot;file/files.txt&quot;</span>).toAbsolutePath());
    <span class="hljs-comment">// &#x5185;&#x5BB9;</span>
    List&lt;String&gt; contents = <span class="hljs-keyword">new</span> ArrayList&lt;&gt;();
    contents.add(<span class="hljs-string">&quot;Hello &quot;</span>);
    contents.add(<span class="hljs-string">&quot;World&quot;</span>);
    <span class="hljs-comment">// &#x5199;&#x6587;&#x4EF6;</span>
    Files.write(path, contents, Charset.forName(<span class="hljs-string">&quot;UTF-8&quot;</span>));
    Files.size(path);
    <span class="hljs-comment">// &#x83B7;&#x53D6;&#x6240;&#x6709;&#x7684;&#x5B57;&#x8282;&#x6570;&#x7EC4;</span>
    <span class="hljs-keyword">byte</span>[] bytes = Files.readAllBytes(path);
    ByteArrayOutputStream output = <span class="hljs-keyword">new</span> ByteArrayOutputStream();
    Files.copy(path, output);
    <span class="hljs-comment">// &#x8F93;&#x51FA;</span>
    System.out.println(<span class="hljs-keyword">new</span> String(bytes));

    Path target = Paths.get(<span class="hljs-string">&quot;file/newfiles.txt&quot;</span>);
    <span class="hljs-comment">// &#x6743;&#x9650; &#x6587;&#x4EF6;&#x8BFB;&#x5199;&#x6743;&#x9650;</span>
    Set&lt;PosixFilePermission&gt; perms = PosixFilePermissions.fromString(<span class="hljs-string">&quot;rw-rw-rw-&quot;</span>);
    <span class="hljs-comment">// &#x6587;&#x4EF6;&#x5C5E;&#x6027;&#x96C6;&#x5408;</span>
    FileAttribute&lt;Set&lt;PosixFilePermission&gt;&gt; attrs=PosixFilePermissions.asFileAttribute(perms);
    <span class="hljs-comment">// &#x521B;&#x5EFA; &#x6709;&#x8BFB;&#x5199;&#x6743;&#x9650;&#x7684; &#x6587;&#x4EF6;</span>
    Files.createFile(target,attrs);
    <span class="hljs-comment">// &#x590D;&#x5236; &#x6587;&#x4EF6;&#x7684;&#x5C5E;&#x6027;&#x4E00;&#x8D77;&#x590D;&#x5236;&#x7684;</span>
    Files.copy(path, target, StandardCopyOption.COPY_ATTRIBUTES);
    <span class="hljs-comment">// &#x79FB;&#x52A8;</span>
    Files.move(path, target);
    <span class="hljs-comment">// &#x5220;&#x9664;</span>
    Files.delete(path);
}
</code></pre>
<footer class="page-footer"><span class="copyright">Copyright @copy tianwyam 2021 all right reserved&#xFF0C;powered by Gitbook</span><span class="footer-modification">&#x6700;&#x540E;&#x4FEE;&#x8BA2;&#x65F6;&#x95F4;&#xFF1A;
2019-10-05 14:33:29
</span></footer>
                                
                                </section>
                            
                        </div>
                    </div>
                
            </div>

            
                
                <a href="java_tree.html" class="navigation navigation-prev " aria-label="Previous page: 二叉树遍历">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="java_exception.html" class="navigation navigation-next " aria-label="Next page: 异常exception">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"java输入输出流","level":"3.1","depth":1,"next":{"title":"异常exception","level":"3.2","depth":1,"path":"java/java_exception.md","ref":"java/java_exception.md","articles":[]},"previous":{"title":"二叉树遍历","level":"2.4.3","depth":2,"path":"java/java_tree.md","ref":"java/java_tree.md","articles":[]},"dir":"ltr"},"config":{"plugins":["-search","-sharing","github","tbfed-pagefooter","splitter","pageview-count","anchor-navigation-ex","chapter-fold","expandable-chapters-small","livereload"],"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"pluginsConfig":{"tbfed-pagefooter":{"copyright":"Copyright @copy tianwyam 2021","modify_label":"最后修订时间：","modify_format":"YYYY-MM-DD HH:mm:ss"},"chapter-fold":{},"github":{"url":"https://github.com/tianwyam"},"livereload":{},"splitter":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"fontsettings":{"theme":"white","family":"sans","size":2},"highlight":{},"anchor-navigation-ex":{"associatedWithSummary":true,"float":{"floatIcon":"fa fa-navicon","level1Icon":"","level2Icon":"","level3Icon":"","showLevelIcon":false},"mode":"float","multipleH1":true,"pageTop":{"level1Icon":"","level2Icon":"","level3Icon":"","showLevelIcon":false},"printLog":false,"showGoTop":false,"showLevel":false},"pageview-count":{},"expandable-chapters-small":{},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"theme":"default","author":"tianwyam","pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"variables":{},"title":"JAVA学习积累","links":{"sidebar":{"博客Blog":"https://blog.csdn.net/mybook201314"}},"gitbook":"*","description":"积累Java学习过程中的知识点，俗话说：好记性不如烂笔头"},"file":{"path":"java/java_io_nio.md","mtime":"2019-10-05T06:33:29.277Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2021-11-03T12:24:01.188Z"},"basePath":"..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="../gitbook/gitbook.js"></script>
    <script src="../gitbook/theme.js"></script>
    
        
        <script src="../gitbook/gitbook-plugin-github/plugin.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-splitter/splitter.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-pageview-count/plugin.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-chapter-fold/chapter-fold.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-expandable-chapters-small/expandable-chapters-small.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-livereload/plugin.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

